﻿var config = require('../config')
var http = require('http')
var https = require('https')
var crypto = require('crypto')

var T = {
  ajax: function (options, res) {
    var tempData = options.data
    if (options.json) {
      options.data = JSON.stringify(options.data)
      options.data = options.data == '{}' ? null : options.data
      options.contentType = options.contentType || 'application/json'
    } else {
      options.data = options.data && T.getObjStr(options.data) || null
      options.contentType = options.contentType || 'application/x-www-form-urlencoded'
    }

    var port = options.port || config.ajaxPort || 80
    var headers = options.headers || {}
    var host = options.host || config.ajaxHost
    headers['Content-Type'] = options.contentType
    headers['Host'] = host

    console.log('【Ajax Send Method】:' + host + options.url)
    console.log('【Ajax Send Params】:' + JSON.stringify(options.data))

    var protocal = options.port == 443 ? https : http
    var ajaxParam = {
      path: options.url || '',
      method: options.method || 'post',
      hostname: host,
      port: port,
      headers: headers
    }

    console.log('当前的ajaxParam为' + ajaxParam)

    var request = protocal.request(ajaxParam, function (result) {
      try {
        var buffers = []
        var allLength = 0
        hasResponse = true
        result.on('data', function (chunk) {
          buffers.push(chunk)
          allLength += chunk.length
        })
        result.on('end', function () {
          console.log('Ajax send OK!' + options.url)

          var bf
          bf = Buffer.concat(buffers, allLength)

          var data = bf.toString()
          var json = {}
          if (data.indexOf('{') === 0 || data.indexOf('[') == 0) {
            json = T.tryEval(data)
            var jsonData
            if (json && typeof json.Data === 'string' && (json.Data.indexOf('{') == 0 || json.Data.indexOf('[') == 0)) {
              jsonData = T.tryEval(json.Data)
              json.Data = jsonData || json.Data
            }
          }
          options.success && options.success(data, json)
        })

        result.on('error', function (err) {
          console.log('error', err)
          options.success && options.success(JSON.stringify({ code: -1, msg: err }))
        })
      } catch (e) {
        console.log('catch：ajax请求失败')
        options.success && options.success(JSON.stringify({ code: -1, msg: e }))
      }
    })

    request.on('error', function (err) {
      console.log('error：ajax请求失败', err)
      options.success && options.success(JSON.stringify({ code: -1, msg: err}))
      if (res) {
        res.end('error')
      }
    })

    if (options.data) {
      request.write(options.data || '')
    }
    request.end()
  },
  getObjStr: function (data) {
    if (typeof data === 'string') {
      return data
    }
    var arr = []
    for (var p in data) {
      if (typeof data[p] === 'object') {
        data[p] = JSON.stringify(data[p])
      }
      arr.push(p + '=' + encodeURIComponent(data[p]))
    }
    return arr.join('&')
  },
  tryEval: function (js) {
    var result = null
    if (!js) {
      return result
    }
    if (typeof js !== 'string') {
      return js
    }
    try {
      result = JSON.parse(js)
    } catch (e) {
      console.log('[eval error json解析错误]' + e.message)
    }
    return result
  },
  getCurrentUrl: function (req) {
    var host = req.hostname
    if (config.developMode) {
      host = host + ':' + config.port
    }
    return req.protocol + '://' + host + req.originalUrl
  },
  getLoginUrl: function (req, isIndex) {
    var host = req.hostname
    if (config.developMode) {
      host = req.hostname + ':' + config.port
    }
    var currentUrl = req.protocol + '://' + host
    if (!isIndex) {
      currentUrl = currentUrl + req.path
    }
    var loginUrl = 'http://' + config.ssoHost + '/login.html?service=productCenter&target=' + encodeURIComponent(currentUrl)
    return loginUrl
  },

  encrypt: function (str) {
    str = str + ''
    var key = 'dEfAu1tS3cretKeY'
    var iv = ''
    var clearEncoding = 'utf8'
    var cipherEncoding = 'base64'
    var cipherChunks = []
    var cipher = crypto.createCipheriv('aes-128-ecb', key, iv)
    cipher.setAutoPadding(true)

    cipherChunks.push(cipher.update(str, clearEncoding, cipherEncoding))
    cipherChunks.push(cipher.final(cipherEncoding))

    return cipherChunks.join('')
  },
  decrypt: function (str) {
    var key = 'dEfAu1tS3cretKeY'
    var iv = ''
    var clearEncoding = 'utf8'
    var cipherEncoding = 'base64'
    var cipherChunks = []
    var decipher = crypto.createDecipheriv('aes-128-ecb', key, iv)
    decipher.setAutoPadding(true)

    cipherChunks.push(decipher.update(str, cipherEncoding, clearEncoding))
    cipherChunks.push(decipher.final(clearEncoding))

    return cipherChunks.join('')
  },
  formatDate: function (format, date) { //   "yyyy-MM-dd hh:mm:ss"
    if (!date) return ''
    date = new Date(date)
    var o = {
      'M+': date.getMonth() + 1, // month
      'd+': date.getDate(), // day
      'h+': date.getHours(), // hour
      'm+': date.getMinutes(), // minute
      's+': date.getSeconds(), // second
      'q+': Math.floor((date.getMonth() + 3) / 3), // quarter
      'S': date.getMilliseconds(), // millisecond
      'w': '日一二三四五六'.charAt(date.getDay())
    }
    format = format.replace(/y{4}/i, date.getFullYear())
      .replace(/y{2}/i, date.getFullYear().toString().substring(2))

    for (var k in o) {
      var reg = new RegExp(k)
      format = format.replace(reg, match)
    }
    function match (m) {
      return m.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
    }
    return format
  }
}

module.exports = T
